declare let eManager;
const {ccclass, property} = cc._decorator;

//6-1 睫毛
@ccclass
export default class JieMao extends cc.Component {
    @property
    stepName = "";
    //左眼 睫毛夹 动画
    @property(cc.Animation)
    leftJiaAni:cc.Animation = null;
    //右眼 睫毛夹 动画
    @property(cc.Animation)
    rightJiaAni:cc.Animation = null;
    //左眼 睫毛刷 动画
    @property(cc.Animation)
    leftShuaAni:cc.Animation = null;
    //右眼 睫毛刷 动画
    @property(cc.Animation)
    rightShuaAni:cc.Animation = null;

    //左眼 睫毛刷 碰撞区
    @property(cc.Node)
    leftShuaPeng:cc.Node = null;
    //右眼 睫毛刷 碰撞区
    @property(cc.Node)
    rightShuaPeng:cc.Node = null;

    //睫毛虚线碰撞区 左
    @property(cc.Node)
    pengLeftNode: cc.Node = null;
    //睫毛虚线碰撞区 右
    @property(cc.Node)
    pengRightNode: cc.Node = null;

    //睫毛效果 右
    @property(cc.Node)
    effectRightNode: cc.Node = null;
    //睫毛效果 左
    @property(cc.Node)
    effectLeftNode: cc.Node = null;
    //效果 上下睫毛
    @property(cc.Node)
    right_up: cc.Node = null;
    @property(cc.Node)
    right_down: cc.Node = null;
    @property(cc.Node)
    left_up: cc.Node = null;
    @property(cc.Node)
    left_down: cc.Node = null;

    //夹睫毛音效
    @property({ type: cc.AudioClip })
    jia_Sound: cc.AudioClip = null;
    //刷睫毛音效
    @property({ type: cc.AudioClip })
    shua_Sound: cc.AudioClip = null;

    //根节点 动画
    @property(cc.Animation)
    rootAni:cc.Animation = null;
    
    _isPlay = false;
    _curPoint = 1;//一共4步  1.右眼睫毛夹  2左眼睫毛夹   3.右眼睫毛刷    4.左眼睫毛刷
    _isDone = false;
    start(){
        //@ts-ignore
        this.rightJiaAni.on("finished",this.rightJiaFinish,this);
        //@ts-ignore
        this.leftJiaAni.on("finished",this.leftJiaFinish,this);
        //@ts-ignore
        this.rightShuaAni.on("finished",this.rightShuaFinish,this);
        //@ts-ignore
        this.leftShuaAni.on("finished",this.leftShuaFinish,this);
        //@ts-ignore
        this.rootAni.on("finished",this.rootFinish,this);
    };
    //初始化
    initData(){
        // eManager.emit("HZ_SHOW_SELECT_LIST", {
        //     step: 6,
        //     point: 1,
        //     // callback:()=>{
        //     //     this.rootAni.play("step4_1_01");
        //     // }
        // });
        this.rootAni.play("step4_1_01");
        this._curPoint = 1;
        this._isPlay = true;
        eManager.emit("REFRESH_STEP_NAME", this.stepName);
        eManager.emit("HZ_REFRESH_ICON_TIPS", null);
    };

    _touchStart(event){
        if(this._isPlay)
            return;
        if(this._curPoint > 4)
            return;
        let worldPoint = event.touch.getLocation();
        if (this._curPoint == 1) {
            let localPoint = this.rightJiaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.rightJiaAni.node.x = localPoint.x;
            this.rightJiaAni.node.y = localPoint.y;
        } else if (this._curPoint == 2) {
            let localPoint = this.rightShuaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.rightShuaAni.node.x = localPoint.x;
            this.rightShuaAni.node.y = localPoint.y - 200;
        } else if (this._curPoint == 3) {
            let localPoint = this.leftJiaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.leftJiaAni.node.x = localPoint.x;
            this.leftJiaAni.node.y = localPoint.y;
        } else if (this._curPoint == 4) {
            let localPoint = this.leftShuaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.leftShuaAni.node.x = localPoint.x;
            this.leftShuaAni.node.y = localPoint.y - 200;
        }
    };

    _touchMove(event:any){
        if(this._isPlay)
            return;
        if(this._curPoint > 4)
            return;
        // let delta = event.touch.getDelta();
        let worldPoint = event.touch.getLocation();
        let peng = null;
        let box = null;
        if(this._curPoint == 1){
            // this.rightJiaAni.node.x += delta.x;
            // this.rightJiaAni.node.y += delta.y;
            let localPoint = this.rightJiaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.rightJiaAni.node.x = localPoint.x;
            this.rightJiaAni.node.y = localPoint.y;
            peng = this.pengRightNode.getBoundingBoxToWorld();
            box = this.rightJiaAni.node.getBoundingBoxToWorld();
        }else if(this._curPoint == 2){
            // this.rightShuaAni.node.x += delta.x;
            // this.rightShuaAni.node.y += delta.y;
            let localPoint = this.rightShuaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.rightShuaAni.node.x = localPoint.x - 100;
            this.rightShuaAni.node.y = localPoint.y - 200;
            peng = this.pengRightNode.getBoundingBoxToWorld();
            box = this.rightShuaPeng.getBoundingBoxToWorld();
        }else if(this._curPoint == 3){
            // this.leftJiaAni.node.x += delta.x;
            // this.leftJiaAni.node.y += delta.y;
            let localPoint = this.leftJiaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.leftJiaAni.node.x = localPoint.x;
            this.leftJiaAni.node.y = localPoint.y;
            peng = this.pengLeftNode.getBoundingBoxToWorld();
            box = this.leftJiaAni.node.getBoundingBoxToWorld();
        }else if(this._curPoint == 4){
            // this.leftShuaAni.node.x += delta.x;
            // this.leftShuaAni.node.y += delta.y;
            let localPoint = this.leftShuaAni.node.parent.convertToNodeSpaceAR(worldPoint);
            this.leftShuaAni.node.x = localPoint.x - 100;
            this.leftShuaAni.node.y = localPoint.y - 200;
            peng = this.pengLeftNode.getBoundingBoxToWorld();
            box = this.leftShuaPeng.getBoundingBoxToWorld();
        }
        if(!peng || !box)
            return;
        if (peng.intersects(box)) {
            this.playAni();
        }
    };

    playAni(){
        this._isPlay = true;
        if(this._curPoint == 1){
            this.rightJiaAni.play("step6_jiemaojia_jiaY_jia");
            eManager.emit("SET_VIBRATE", 300);
            cc.audioEngine.play(this.jia_Sound, false, 4);
        }else if(this._curPoint == 2){
            this.rightShuaAni.play("step6_jiemaojia_jiaY_bi");
            cc.audioEngine.play(this.shua_Sound, false, 4);
            eManager.emit("SET_VIBRATE", 300);
        }
        else if(this._curPoint == 3){
            this.leftJiaAni.play("step6_jiemaojia_jiaZ_jia");
            cc.audioEngine.play(this.jia_Sound, false, 4);
            eManager.emit("SET_VIBRATE", 300);
        }
        else if(this._curPoint == 4){
            this.leftShuaAni.play("step6_jiemaojia_jiaZbi");
            cc.audioEngine.play(this.shua_Sound, false, 4);
            eManager.emit("SET_VIBRATE", 300);
        }
    };

    //右睫毛夹 动画
    rightJiaFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if (animState.name == "step6_jiemaojia_jiaY_jinru"){
            this._isPlay = false;
            this.pengRightNode.active = true;
        }else if (animState.name == "step6_jiemaojia_jiaY_jia"){
            this._isPlay = true;
            // this.rightJiaAni.node.active = false;
            this.rightShuaAni.node.active = true;
            this.rightShuaAni.play("step6_jiemaojia_biY_jinru");
        }
    };
    //左睫毛夹 动画
    leftJiaFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if (animState.name == "step6_jiemaojia_jiaZ_jinru"){
            this._isPlay = false;
            this._curPoint ++;
            this.pengLeftNode.active = true;
        }else if (animState.name == "step6_jiemaojia_jiaZ_jia"){
            this._isPlay = true;
            // this.leftJiaAni.node.active = false;
            this.leftShuaAni.node.active = true;
            this.leftShuaAni.play("step6_jiemaojia_biZ_jinru");
        }
    };

    //右睫毛刷 动画
    rightShuaFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if (animState.name == "step6_jiemaojia_biY_jinru"){
            this._curPoint ++;
            this._isPlay = false;
        }else if (animState.name == "step6_jiemaojia_jiaY_bi"){
            this._isPlay = true;
            this.rightShuaAni.node.active = false;
            this.pengRightNode.active = false;
            // this.right_up.active = false;
            // this.right_down.active = false;
            this.effectRightNode.active = true;
            this.rootAni.play("step4_1_02");
        }
    };
    //左睫毛刷 动画
    leftShuaFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if (animState.name == "step6_jiemaojia_biZ_jinru"){
            this._curPoint ++;
            this._isPlay = false;
            this.pengLeftNode.active = true;
        }else if (animState.name == "step6_jiemaojia_jiaZbi"){
            this.leftShuaAni.node.active = false;
            this.pengLeftNode.active = false;
            // this.left_up.active = false;
            // this.left_down.active = false;
            this.effectLeftNode.active = true;
            this.rootAni.play("step4_1_03");
            eManager.emit("DONE_STEP", 6);
            eManager.emit("MINI_DONE_POINT", 4);
            eManager.emit("HZ_MAKE_DOT", {
                makeup_close:6,
                makeup_step:this.stepName
            });
        }
    };

    rootFinish(event: cc.Event.EventCustom, animState: cc.AnimationState){
        if(animState.name == "step4_1_01"){
            this.rightJiaAni.node.active = true;
            this.rightJiaAni.play("step6_jiemaojia_jiaY_jinru");
        }
        else if(animState.name == "step4_1_02"){
            this.leftJiaAni.node.active = true;
            this.leftJiaAni.play("step6_jiemaojia_jiaZ_jinru");
        }
    };
}
